// 获取登录标题元素
let singInTitle = document.querySelector('#sing-in-title');
// 获取注册标题元素
let registerTitle = document.querySelector('#register-title');
// 获取登录按钮元素
let singInSpan = document.querySelector('#sing-in-span')
// 获取注册按钮元素
let registerSpan = document.querySelector('#register-span');
// 获取登录按钮元素
let singInButton = document.querySelector('#sing-in-button');
// 获取注册按钮元素
let registerButton = document.querySelector('#register-button');
// 获取登录输入框元素
let signinInputBox = document.querySelector('.signin-input-box');
// 获取注册输入框元素
let registerInputBox = document.querySelector('.register-input-box');
// 获取shell元素
let shell = document.querySelector('.shell');
//获取邮箱值
let username = document.querySelector('#signin-email');
//获取密码值
let password = document.querySelector('#signin-password')

let email = document.querySelector('#register-email');
let regUsername = document.querySelector('#register-Name');
let regPassword = document.querySelector('#register-password');
// 登录按钮点击事件
singInSpan.onclick = function (e) {
    goToRegister();
}
// 注册按钮点击事件
registerSpan.onclick = function (e) {
    goToSignIn();
};

// 跳转到注册页面
let goToRegister = function () {
    // 登录标题淡入动画
    singInTitle.style.animation = 'run .4s forwards'
    // 注册标题淡出动画
    registerTitle.style.animation = 'return .4s forwards'
    // 登录按钮淡入动画
    singInSpan.style.animation = 'run .4s forwards'
    // 注册按钮淡出动画
    registerSpan.style.animation = 'return .4s forwards'

    // 登录按钮淡入动画
    singInButton.style.animation = 'run .4s forwards'
    // 注册按钮淡出动画
    registerButton.style.animation = 'return .4s forwards'

    // 登录输入框淡入动画
    signinInputBox.style.animation = 'inputRun .3s forwards'
    // 注册输入框淡出动画
    registerInputBox.style.animation = 'inputComeBack .3s forwards'

    // shell淡入动画
    shell.style.animation = 'shell .3s linear'
}

// 跳转到登录页面
let goToSignIn = function () {
    // 登录标题淡出动画
    singInTitle.style.animation = 'run1 .4s forwards'
    // 注册标题淡入动画
    registerTitle.style.animation = 'return1 .4s forwards'

    // 登录按钮淡出动画
    singInSpan.style.animation = 'run1 .4s forwards'
    // 注册按钮淡入动画
    registerSpan.style.animation = 'return1 .4s forwards'

    // 登录按钮淡出动画
    singInButton.style.animation = 'run1 .4s forwards'
    // 注册按钮淡入动画
    registerButton.style.animation = 'return1 .4s forwards'

    // 登录输入框淡出动画
    signinInputBox.style.animation = 'inputRun1 .3s forwards'
    // 注册输入框淡入动画
    registerInputBox.style.animation = 'inputComeBack1 .3s forwards'

    // shell淡出动画
    shell.style.animation = 'shell1 .3s linear'
}

// 登录按钮点击事件
singInButton.onclick = function (e) {
    // 阻止默认事件
    e.preventDefault();
    sendInfo(username.value, password.value);
}
// 注册按钮点击事件
registerButton.onclick = function (e) {
    // 阻止默认事件
    e.preventDefault();
    sendRegInfo(regUsername.value, regPassword.value, email.value);
}

// 发送登录信息函数
function sendInfo(username, password) {
    axios({
        method: 'post',
        url: '/api/login',
        data: {
            username,
            password
        }
    }).then(res => {
        if (res.data.status === 0) {
            swal("登录成功", "即将跳转到首页", "success");

            setTimeout(function () {
                // 将token存储到本地
                localStorage.setItem('token', res.data.token);
                // 跳转到首页
                location.href = '/public/seat/index.html';
            }, 1000);

        } else {
            alert(res.data.message);
        }
    }).catch(err => {
        console.log(err);
    });
}

function sendRegInfo(username, password, email) {
    console.log(username, password, email);
    axios(
        {
            method: 'post',
            url: '/api/reguser',
            data: {
                username,
                password,
                email
            }
        }
    ).then(res => {
        if (res.data.status === 0) {
            alert('注册成功');
            // 跳转到登录页面
            goToSignIn();
        } else {
            alert(res.data.message);
        }
    }).catch(err => {
        console.log(err);
    }
    )
}

//跳转到首页token有效
axios.get('/my/userinfo', {
    headers: {
        Authorization: localStorage.getItem('token')
    }
}).then((res) => {
    if (res.data.status === 0) {
        location.href = '/public/seat/index.html';
    }
}).catch(err => {
    console.log(err);
});
